package com.gosling.cloudspace.aop;

import cn.hutool.core.util.IdUtil;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * @author zhushengxian
 * @date 2022/12/28 0028
 * @description:
 */
@Aspect
@Component
public class TraceIdAspect {

    /**
     * 定义scheduled切点
     */
    @Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void scheduledAspect() {
    }

//    /**
//    * 定义kafkaListener切点
//    */
//    @Pointcut("@annotation(org.springframework.kafka.annotation.KafkaListener)")
//    public void kafkaListenerAspect() {
//    }

//    @Pointcut("execution(* com.soyoo.yooloan.module.user.mq.*Listener.onMessage(..))")
//    public void rocketMQMessage() {}

//    @Before("scheduledAspect() || kafkaListenerAspect()")
//    public void addTradeId() {
//        MDC.put("traceId", IdUtil.fastUUID());
//    }
//
//    @After("scheduledAspect() || kafkaListenerAspect()")
//    public void clearMdc() {
//        MDC.clear();
//    }

    @Before("scheduledAspect() ")
    public void addTradeId() {
        MDC.put("traceId", IdUtil.fastUUID());
    }

    @After("scheduledAspect() ")
    public void clearMdc() {
        MDC.clear();
    }
}
